package elements.axis ;
import flash.text.TextField;
import flash.display.Sprite;
import flash.text.TextFormat;
import string.Utils;
import flash.geom.Point;
import ColorConverter.Color;

typedef RADARSPOKELABELSTYLESTRUCT = {
	var color		: Color;
	var text		: String;
	var size		: Int;
};

class RadarSpokeLabels extends Sprite {

	var style : {
		color		: Color
		, labels	: Array<String>
	};
	public var labels:Array<String>;
	
	
	public function new( json:Dynamic ) {
		super();
		// default values
		this.style = {
			labels:			new Array<String>()
			, color:		Hex('#784016')
		};
		
		if( json != null )
			ObjectHelper.merge_2( json, this.style );
			
		// //tr.ace_json(this.style);
			
		//this.style.color = Utils.get_color( this.style.color );
		
		// cache the text for tooltips
		this.labels = new Array<String>();
		var values:Array<Dynamic>;
		var ok:Bool = false;
		
		if(Std.is( this.style.labels,Array ) &&  this.style.labels.length > 0 )
		{
			for ( s in this.style.labels )
				this.add( s, this.style );
		}
		
	}

	public function add( label:Dynamic, style:Dynamic ) : Void
	{
		var label_style:RADARSPOKELABELSTYLESTRUCT = {
			color:		style.color,
			text:		'',
			size:       11
		};

		if(Std.is(label,String ))
			label_style.text = cast(label,String);
		else {
			ObjectHelper.merge_2( label, label_style );
		}

		// our parent color is a number, but
		// we may have our own color:
		//if(Std.is(label_style.color,String ))
		//	label_style.color = Utils.get_color( label_style.color );

		this.labels.push( label_style.text );

		var l:TextField = this.make_label( label_style );
		this.addChild( l );
	}
	
	public function make_label( label_style:Dynamic ):TextField {
		
		// we create the text in its own movie clip
		
		var tf:TextField = new TextField();
		tf.x = 0;
		tf.y = 0;
		
		var tmp:Array<String> = label_style.text.split( '<br>' );
		var text:String = tmp.join('\n');
		
		tf.text = text;
		
		var fmt:TextFormat = new TextFormat();
		fmt.color = ColorConverter.toFloat(label_style.color);
		fmt.font = "Verdana";
		fmt.size = label_style.size;
		fmt.align = flash.text.TextFormatAlign.RIGHT;
		
		tf.setTextFormat(fmt);
		tf.autoSize = flash.text.TextFieldAutoSize.LEFT;
		tf.visible = true;
		
		return tf;
	}
	
	// move y axis labels to the correct x pos
	public function resize( sc:ScreenCoordsRadar ):Void {

		var tf:TextField;
		//
		// loop over the lables and make sure they are on the screen,
		// reduce the radius until they fit
		//
		var i: Int = 0;
		var outside:Bool;
		do
		{
			outside = false;
			this.resize_2( sc );
			
			for ( i in 0...this.numChildren )
			{
				tf = cast(this.getChildAt(i),TextField);
				if( (tf.x < sc.left) ||
					(tf.y < sc.top) ||
					(tf.y + tf.height > sc.bottom ) ||
					(tf.x + tf.width > sc.right)
				)
					outside = true;
			
			}
			sc.reduce_radius();
		} while ( outside && sc.get_radius() > 10 );
		//
		//
		//
	}
	
		function resize_2( sc:ScreenCoordsRadar ):Void {
		
		var i: Int;
		var tf:TextField;
		var mid_x: Float = sc.get_center_x();
		
		// now move it to the correct Y, vertical center align
		for ( i in 0...this.numChildren ) {
			
			tf = cast(this.getChildAt(i),TextField);

			var p:flash.geom.Point = sc.get_get_x_from_pos_and_y_from_val( i, sc.get_max() );
			if ( p.x > mid_x )
				tf.x = p.x;					// <-- right align the text
			else
				tf.x = p.x - tf.width;		// <-- left align the text
			
			if ( i == 0 ) {
				//
				// this is the top label and will overwrite
				// the radius label -- so we right align it
				// because the radius labels are left aligned
				//
				tf.y = p.y - tf.height;
				tf.x = p.x;
			}
			else
				tf.y = p.y;
		}
	}
	
	public function die(): Void {
		
		this.style = null;
		this.labels = null;
		
		this.graphics.clear();
		while ( this.numChildren > 0 )
			this.removeChildAt(0);
	}
}
